﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

public class DAL_tbl_qcsicomill_ssa
{
    public DAL_tbl_qcsicomill_ssa()
    {

    }



    public List<tbl_qcsicomill_ssa> select(String SSA_ID)
    {
        List<tbl_qcsicomill_ssa> list = new List<tbl_qcsicomill_ssa>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select SSA_ID,Work_Order_Number,DRUM_NUMBER,Budgeted_Qty,Actual_Qty,SSA,Remark,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_ssa where SSA_ID=@SSA_ID";
        cmd.Parameters.AddWithValue("@SSA_ID", SSA_ID);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_ssa()
            {
                SSA_ID = dr["SSA_ID"].ToString(),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_NUMBER = dr["DRUM_NUMBER"].ToString(),
                Budgeted_Qty = Convert.ToInt32(dr["Budgeted_Qty"]),
                Actual_Qty = Convert.ToInt32(dr["Actual_Qty"]),
                SSA = Convert.ToDecimal(dr["SSA"]),
                Remark = dr["Remark"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public List<tbl_qcsicomill_ssa> selectAll()
    {
        List<tbl_qcsicomill_ssa> list = new List<tbl_qcsicomill_ssa>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select SSA_ID,Work_Order_Number,DRUM_NUMBER,Budgeted_Qty,Actual_Qty,SSA,Remark,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_ssa Order By SSA_ID";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_ssa()
            {
                SSA_ID = dr["SSA_ID"].ToString(),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_NUMBER = dr["DRUM_NUMBER"].ToString(),
                Budgeted_Qty = Convert.ToInt32(dr["Budgeted_Qty"]),
                Actual_Qty = Convert.ToInt32(dr["Actual_Qty"]),
                SSA = Convert.ToDecimal(dr["SSA"]),
                Remark = dr["Remark"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }

    public DataTable selectAllForGrid()
    {
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = @"Select SSA_ID,Work_Order_Number,DRUM_NUMBER,Budgeted_Qty,Actual_Qty,SSA,Remark,CASE WHEN IS_ACTIVE =1 THEN 'True' ELSE 'False' END as 'IS_ACTIVE',CREATED_BY,CREATED_DATE as CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_ssa Order By SSA_ID desc";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        return dt;
    }

    public bool insert(List<tbl_qcsicomill_ssa> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Insert into tbl_qcsicomill_ssa(Work_Order_Number, DRUM_NUMBER,Budgeted_Qty ,Actual_Qty, SSA, Remark, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_NUMBER,@Budgeted_Qty,@Actual_Qty, @SSA, @Remark, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        foreach (tbl_qcsicomill_ssa row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_NUMBER", row.DRUM_NUMBER);
            cmd.Parameters.AddWithValue("@Budgeted_Qty", row.Budgeted_Qty);
            cmd.Parameters.AddWithValue("@Actual_Qty", row.Actual_Qty);
            cmd.Parameters.AddWithValue("@SSA", row.SSA);
            cmd.Parameters.AddWithValue("@Remark", row.Remark);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);
            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }

    public int insert(String Work_Order_Number, String DRUM_NUMBER, int Budgeted_Qty, int Actual_Qty, Decimal SSA, String Remark, bool IS_ACTIVE, String CREATED_BY, DateTime CREATED_DATE)
    {
        int i = 0;
        string sql = @"Insert into tbl_qcsicomill_ssa(Work_Order_Number, DRUM_NUMBER,Budgeted_Qty ,Actual_Qty, SSA, Remark, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_NUMBER,@Budgeted_Qty,@Actual_Qty, @SSA, @Remark, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_NUMBER", DRUM_NUMBER);
        cmd.Parameters.AddWithValue("@Budgeted_Qty", Budgeted_Qty);
        cmd.Parameters.AddWithValue("@Actual_Qty", Actual_Qty);
        cmd.Parameters.AddWithValue("@SSA", SSA);
        cmd.Parameters.AddWithValue("@Remark", Remark);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
        cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }


    public bool update(List<tbl_qcsicomill_ssa> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Update tbl_qcsicomill_ssa SET Work_Order_Number=@Work_Order_Number, DRUM_NUMBER=@DRUM_NUMBER, Budgeted_Qty=@Budgeted_Qty, Actual_Qty=@Actual_Qty, SSA=@SSA, Remark=@Remark, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE SSA_ID=@SSA_ID";

        foreach (tbl_qcsicomill_ssa row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@SSA_ID", row.SSA_ID);
            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_NUMBER", row.DRUM_NUMBER);
            cmd.Parameters.AddWithValue("@Budgeted_Qty", row.Budgeted_Qty);
            cmd.Parameters.AddWithValue("@Actual_Qty", row.Actual_Qty);
            cmd.Parameters.AddWithValue("@SSA", row.SSA);
            cmd.Parameters.AddWithValue("@Remark", row.Remark);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }


    public int update(String SSA_ID, String Work_Order_Number, String DRUM_NUMBER, int Budgeted_Qty, int Actual_Qty, Decimal SSA, String Remark, bool IS_ACTIVE, String UPDATED_BY, DateTime UPDATED_DATE)
    {
        int i = 0;
        string sql = @"Update tbl_qcsicomill_ssa SET Work_Order_Number=@Work_Order_Number, DRUM_NUMBER=@DRUM_NUMBER, Budgeted_Qty=@Budgeted_Qty, Actual_Qty=@Actual_Qty, SSA=@SSA, Remark=@Remark, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE SSA_ID=@SSA_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@SSA_ID", SSA_ID);
        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_NUMBER", DRUM_NUMBER);
        cmd.Parameters.AddWithValue("@Budgeted_Qty", Budgeted_Qty);
        cmd.Parameters.AddWithValue("@Actual_Qty", Actual_Qty);
        cmd.Parameters.AddWithValue("@SSA", SSA);
        cmd.Parameters.AddWithValue("@Remark", Remark);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
        cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int delete(String SSA_ID)
    {
        int i = 0;
        string sql = "Delete FROM tbl_qcsicomill_ssa WHERE SSA_ID=@SSA_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@SSA_ID", SSA_ID);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }


    public List<tbl_qcsicomill_ssa> ddlWorkOrderFor635_Mess()
    {
        List<tbl_qcsicomill_ssa> list = new List<tbl_qcsicomill_ssa>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Work_Order_Number from tbl_qcsicomill_ssa Order By Work_Order_Number";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_ssa()
            {

                Work_Order_Number = dr["Work_Order_Number"].ToString(),

            });
        }

        return list;
    }


    public List<tbl_qcsicomill_ssa> ddlDrumNumber(string WorkOrderNumber)
    {
        List<tbl_qcsicomill_ssa> list = new List<tbl_qcsicomill_ssa>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Drum_Number from tbl_qcsicomill_ssa where Work_Order_Number=@Work_Order_Number and Drum_Number<>'' order by Drum_Number";
        cmd.Parameters.AddWithValue("@Work_Order_Number", WorkOrderNumber);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_ssa()
            {
                DRUM_NUMBER = dr["Drum_Number"].ToString()
            });
        }

        return list;
    }


}